package cn.hill4j.tool.spring.ext.core;

import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
  * ClassName: PackageScanRegistrar
  * Description: 根据包扫描扩展的spring beanDefinition信息注册器超类
  * @author hillchen
  * Date 2023/8/8
  * @version: 1.0
  */
public abstract class PackageScanRegistrar extends AbstractRegistrar{
    @Override
    protected Set<String> getBasePackages(AnnotationMetadata importingClassMetadata) {
        Map<String, Object> attributes = importingClassMetadata
                .getAnnotationAttributes(PackageScanner.class.getCanonicalName());
        Set<String> basePackages = new HashSet<>();
        for (String pkg : (String[]) attributes.get("basePackages")) {
            if (StringUtils.hasText(pkg)) {
                basePackages.add(pkg);
            }
        }
        if (basePackages.isEmpty()) {
            basePackages.add(
                    ClassUtils.getPackageName(importingClassMetadata.getClassName()));
        }
        return basePackages;
    }
}
